using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace gpcx
{
    public partial class MenuFrm : Form
    {
        public Action ShowTopFrm;
        public Action ShowSettingFrm;
        public Action Refdata;
        public Action AppExit;

        public MenuFrm()
        {
            InitializeComponent();
            toolStripMenuItem2.Click += toolStripMenuItem2_Click;
            toolStripMenuItem3.Click += toolStripMenuItem2_Click;
            toolStripMenuItem4.Click += toolStripMenuItem2_Click;
            toolStripMenuItem5.Click += toolStripMenuItem2_Click;
            toolStripMenuItem6.Click += toolStripMenuItem2_Click;
            toolStripMenuItem7.Click += toolStripMenuItem2_Click;
            toolStripMenuItem8.Click += toolStripMenuItem2_Click;
            toolStripMenuItem9.Click += toolStripMenuItem2_Click;
            toolStripMenuItem10.Click += toolStripMenuItem2_Click;
            toolStripMenuItem11.Click += toolStripMenuItem2_Click;
        }

        private void toolStripMenuItem2_Click(object sender, System.EventArgs e)
        {
            Opacity = double.Parse((sender as ToolStripMenuItem)?.Text.Replace("%", "")) / 100;
            ConfigManager.UpdateSetting("MenuFrmOpacity", $"{Opacity}");
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                var opacity = ConfigManager.GetAppSettingsKeyValue("MenuFrmOpacity");
                Opacity = double.Parse(opacity);
            }
            catch
            {
                ConfigManager.UpdateSetting("MenuFrmOpacity", $"{Opacity}");
            }
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            Close();
            AppExit();
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            Hide();
            ShowTopFrm?.Invoke();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Hide();
            ShowSettingFrm?.Invoke();
        }

        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            Refdata?.Invoke();
        }

        private void MenuFrm_Deactivate(object sender, EventArgs e)
        {
            Hide();

        }

        private void listBox1_Click(object sender, EventArgs e)
        {

        }

        public void UpdateData(List<ValueTuple<string, string, string,string,string,string,string>> value)
        {
            this.Invoke((EventHandler)delegate
            {
                if (flowLayoutPanel1.Controls.Count > 0 && Visible == false)
                    return;

                while (flowLayoutPanel1.Controls.Count > value.Count)
                {
                    flowLayoutPanel1.Controls.RemoveAt(value.Count);
                }
                for (var i = 0; i < value.Count; i++)
                {
                    if (i >= flowLayoutPanel1.Controls.Count)
                    {
                        var lvi = new CodeDetialUcFrmV();
                        lvi.SetValue(value[i]);
                        flowLayoutPanel1.Controls.Add(lvi);
                    }
                    else
                    {
                        var lvi = (CodeDetialUcFrmV)flowLayoutPanel1.Controls[i];
                        lvi.SetValue(value[i]);
                    }
                }
                var row =(int)Math.Ceiling(flowLayoutPanel1.Controls.Count / 3.0);
                if (row <= 1)
                    Height = 25 + 44;
                else
                    Height = 25 + 44 + 88 * (row - 1);
            });
        }
    }
}
